package com.dc.juc.example._12_future;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Description: FutureTask
 * @author: gdc
 * @date: 2021/8/17
 * @version 1.0
 */
@Slf4j
public class _2_FutureTask {

    @SneakyThrows
    public static void main(String[] args){
        log.info("初始化...");

        // 创建一个 FutureTask
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            TimeUnit.SECONDS.sleep(3);

            log.info("执行callable方法中的业务...");

            // 返回结果
            return "Done";
        });

        new Thread(futureTask).start();

        TimeUnit.SECONDS.sleep(5);
        log.info("执行主方法中的业务...");

        String result = futureTask.get();
        log.info("获取Callable的返回结果:{}", result);
    }
}
